package com.railway.switchs.action.business;

import com.railway.switchs.aspect.OperationLog;
import com.railway.switchs.base.BaseResponse;
import com.railway.switchs.constant.SystemConstant;
import com.railway.switchs.domain.dto.business.DeviceInfo;
import com.railway.switchs.domain.query.business.DeviceInfoQuery;
import com.railway.switchs.enums.EnumReturnResult;
import com.railway.switchs.enums.EnumRunType;
import com.railway.switchs.service.business.IDeviceInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @ClassName DeviceInfoAction
 * @Author 魏梓晟
 * @Version 1.0
 **/
@Controller
@Slf4j
@RequestMapping("/back/deviceInfo")
public class DeviceInfoAction {

    @Autowired
    private IDeviceInfoService deviceInfoService;

    /**
     * 进入设备信息列表
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String list(){
        return "deviceInfo/list";
    }

    /**
     * 获取设备信息数据
     * @param query
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getList",method = RequestMethod.POST)
    public DeviceInfoQuery getList(DeviceInfoQuery query) {
        try {
            query = deviceInfoService.query(query);
            query.setCode(EnumReturnResult.RETURN_200.getCode());
        } catch (Exception e) {
            log.error("获取设备信息列表数据异常", e);
            query.setCode(EnumReturnResult.RETURN_404.getCode());
            query.setMsg("获取设备信息数据异常");
        }
        return query;
    }

    /**
     * 进入新增页面
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String preAdd(){
        return "deviceInfo/add";
    }

    /**
     * 新增设备信息
     * @param deviceInfo
     * @param result
     * @return
     */
    @ResponseBody
    @OperationLog(billType ="5",actionType ="0" )
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public BaseResponse add(@Validated DeviceInfo deviceInfo, BindingResult result) {
        BaseResponse response = new BaseResponse();
        try {
            if(result.hasErrors()){
                response.setCode(EnumReturnResult.RETURN_404.getCode());
                response.setMsg("DeviceInfo-add参数有误");
                log.info("DeviceInfo-add参数有误："+result.getAllErrors().toString());
                return response;
            }
            deviceInfo.setRunType(EnumRunType.UNUSED.getCode());
            deviceInfoService.add(deviceInfo);
            response.setCode(EnumReturnResult.RETURN_200.getCode());
            response.setMsg("新增成功");
        } catch (Exception e) {
            log.error("新增设备信息异常",e);
            response.setCode(EnumReturnResult.RETURN_404.getCode());
            response.setMsg("新增失败");
        }
        return response;
    }

    /**
     * 进入修改界面
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public ModelAndView preEdit(Long id){
        ModelAndView mav = new ModelAndView();
        try{
            DeviceInfo deviceInfo = deviceInfoService.get(id);
            mav.addObject("deviceInfo",deviceInfo);
            mav.setViewName("deviceInfo/edit");
        }catch (Exception e){
            log.error("获取设备信息详情失败",e);
            mav.addObject(SystemConstant.ERROR_KEY,"系统异常");
            mav.setViewName("deviceInfo/list");
        }
        return mav;
    }

    /**
     * 修改设备信息
     * @param deviceInfo
     * @param result
     * @return
     */
    @ResponseBody
    @OperationLog(billType ="5",actionType ="1" )
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public BaseResponse edit(@Validated DeviceInfo deviceInfo, BindingResult result) {
        BaseResponse response = new BaseResponse();
        try {
            if(result.hasErrors()){
                response.setCode(EnumReturnResult.RETURN_404.getCode());
                response.setMsg("DeviceInfo-edit参数有误");
                log.info("DeviceInfo-edit参数有误："+result.getAllErrors().toString());
                return response;
            }
            DeviceInfo deviceInfoTemp = deviceInfoService.get(deviceInfo.getId());
            if(!EnumRunType.UNUSED.getCode().equals(deviceInfoTemp.getRunType())){
                response.setCode(EnumReturnResult.RETURN_404.getCode());
                response.setMsg("使用中的设备信息不允许修改");
                return response;
            }
            deviceInfoService.edit(deviceInfo);
            response.setCode(EnumReturnResult.RETURN_200.getCode());
            response.setMsg("修改成功");
        } catch (Exception e) {
            log.error("修改设备信息异常",e);
            response.setCode(EnumReturnResult.RETURN_404.getCode());
            response.setMsg("修改失败");
        }
        return response;
    }

    /**
     * 删除设备信息
     * @param id
     * @return
     */
    @ResponseBody
    @OperationLog(billType ="5",actionType ="2" )
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public BaseResponse delete(Long id) {
        BaseResponse response = new BaseResponse();
        try {
            DeviceInfo deviceInfo = deviceInfoService.get(id);
            if(!EnumRunType.UNUSED.getCode().equals(deviceInfo.getRunType())){
                response.setCode(EnumReturnResult.RETURN_404.getCode());
                response.setMsg("使用中的设备信息不允许删除");
                return response;
            }
            deviceInfoService.delete(id);
            response.setCode(EnumReturnResult.RETURN_200.getCode());
            response.setMsg("删除设备信息成功");
        } catch (Exception e) {
            log.error("删除设备信息异常",e);
            response.setCode(EnumReturnResult.RETURN_404.getCode());
            response.setMsg("删除设备信息失败");
        }
        return response;
    }

    /**
     * 获取设备编号数据
     * @param query
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAllList",method = RequestMethod.POST)
    public DeviceInfoQuery getAllList(DeviceInfoQuery query) {
        try {
            query.setRunType(EnumRunType.UNUSED.getCode());
            query.setData(deviceInfoService.getList(query));
            query.setCode(EnumReturnResult.RETURN_200.getCode());
        } catch (Exception e) {
            log.error("获取设备编号数据异常", e);
            query.setCode(EnumReturnResult.RETURN_404.getCode());
            query.setMsg("获取设备编号异常");
        }
        return query;
    }


}
